package com.amethyst.mybatisplus.common;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author Amethyst
 * @date 2021/11/11
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@ApiModel
public class ResponseResult<T> {
    @ApiModelProperty(value = "返回码")
    private Integer code;

    @ApiModelProperty(value = "返回消息")
    private String message;

    @ApiModelProperty(value = "返回数据")
    private T data;

    public void parseEnum(CodeEnum codeEnum) {
        this.code = codeEnum.getCode();
        this.message = codeEnum.getMessage();
    }

    public static <T> ResponseResult<T> status(CodeEnum codeEnum) {
        ResponseResult<T> responseResult = new ResponseResult<>();
        responseResult.parseEnum(codeEnum);
        return responseResult;
    }

    public static <T> ResponseResult<T> ok() {
        return status(CodeEnum.SUCCESS);
    }

    public static <T> ResponseResult<T> ok(T data) {
        ResponseResult<T> responseResult = ok();
        responseResult.setData(data);
        return responseResult;
    }

    public static <T> ResponseResult<T> error(CodeEnum codeEnum) {
        return status(codeEnum);
    }

    public static <T> ResponseResult<T> error(CodeEnum codeEnum, String message) {
        ResponseResult<T> responseResult = error(codeEnum);
        responseResult.setMessage(message);
        return responseResult;
    }

    public static <T> ResponseResult<T> error(CodeEnum codeEnum, T data) {
        ResponseResult<T> responseResult = error(codeEnum);
        responseResult.setData(data);
        return responseResult;
    }

    public static <T> ResponseResult<T> error(Integer code, String message) {
        ResponseResult<T> responseResult = new ResponseResult<>();
        responseResult.setCode(code);
        responseResult.setMessage(message);
        return responseResult;
    }
}

